package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiNewsService;
import com.web.exiaodao.remote.vo.NewsVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author eason.zt
 * @version V1.01
 * @Title: ApiNewsController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 咨询信息接口;新闻接口
 * @date 2016年10月26日 下午1:00:00
 */
@Api(value = "News", description = "咨询接口", position = 10)
@Controller
@RequestMapping("/api/news")
public class ApiNewsController extends BaseController {

    @Autowired
    private ApiNewsService newsService;

    /**
     * @return
     * @Title: findNewsList
     * @Description: 查询所有咨询信息
     */
    @ApiOperation(value = "查询所有咨询", notes = "查询所有咨询信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findNewsList", method = RequestMethod.POST)
    public XaResult<List<NewsVo>> findNewsList(
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("type,字段名:type(类别:1:热点 2：展会，3：政策，4：国际，5:电子书<预留>,type活的，动态返回)") @RequestParam("type") Long type
    ) throws BusinessException {
        XaResult<List<NewsVo>> xr = new XaResult<List<NewsVo>>();
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = null;
        if (type == 1) {
            filterParams = WebUitl.getParametersStartingWith("{'search_EQ_isHot':'" + 1 + "'}", "search_");
        } else {
            filterParams = WebUitl.getParametersStartingWith("{'search_EQ_type':'" + type + "'}", "search_");
        }
        return newsService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
    }

    /**
     * @param modelId
     * @return
     * @Title: findNewsById
     * @Description: 根据ID查找单条实体
     */
    @ApiOperation(value = "根据tId查询咨询", notes = "查询咨询详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findNewsById", method = RequestMethod.POST)
    public XaResult<NewsVo> findNewsById(
            @ApiParam("编号,字段名:modelId,必填") @RequestParam(value = "modelId") Long modelId
    ) throws BusinessException {
        XaResult<NewsVo> xr = new XaResult<NewsVo>();
        try {
            return newsService.findOne(modelId);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage());
            } else {
                xr.error(XaConstant.Message.error);
            }
        }
        return xr;
    }
}

